package org.minos.edit.controller
{
	import flash.display.Loader;
	import flash.filesystem.File;
	import org.aswing.VectorListModel;
	import org.minos.edit.controller.manager.FileManager;
	import org.minos.edit.EditFacate;
	import org.minos.edit.model.AssetsProxy;
	import org.minos.edit.model.ClearProxy;
	import org.minos.edit.view.components.InitInfoPane;
	import org.minos.edit.view.InfoPaneMediator;
	import org.minos.edit.view.InitInfoMediator;
	import org.minos.edit.view.TableMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class ReadFileCommand extends SimpleCommand
	{
		
		override public function execute( notification : INotification ) : void
		{
			super.execute( notification );
			
			var file : File = notification.getBody() as File;
			var xml : XML = new XML( FileManager.readUTFBytes( file ) );
			//xml
			InfoPaneMediator( facade.retrieveMediator( InfoPaneMediator.NAME ) ).info.setXML( xml.config );
			TableMediator( facade.retrieveMediator( TableMediator.NAME ) ).table.setXML( xml.items );
			InitInfoMediator( facade.retrieveMediator( InitInfoMediator.NAME ) ).initInfo.setXML( xml.items );
			//
			var path : String = null;
			if ( xml.config.@classPath != "" && xml.config.@classPath != null )
				path = xml.config.@classPath;
			AssetsProxy( facade.retrieveProxy( AssetsProxy.NAME ) ).getAssets( FileManager.getFile( "root" , path , true ) );
			//
			var info : InitInfoPane = InitInfoMediator( facade.retrieveMediator( InitInfoMediator.NAME ) ).initInfo;
			var ids : Array = String( xml.items.@init ).split( "," );
			var list : VectorListModel = TableMediator( facade.retrieveMediator( TableMediator.NAME ) ).table.getList();
			var ary : Array = [];
			for each ( var j : Object in ids )
			{
				if ( j != "" )
					ary.push( list.getElementAt( int( j ) ) );
			}
			info.childrenAry = ary;
			//set file
			FileManager.setCurrentFile( file );
			sendNotification( EditFacate.FILE_CHANGED , "open" );
		}
	
	}

}